﻿using System;
using System.Collections.Generic;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.TextFormatting;
using BestCode.Creds.Business;
using BestCode.Creds.Entities;
using BestCode.Wpf.ViewModel.Data;

namespace BestCode.Creds.UI.ViewModel
{
    public class VmLayout : DataModelBase<Layout>
    {
        public event EventHandler OnSaved;

        private readonly LayoutBusiness business;
        private readonly Template Template;

        private List<string> allFields;
        private List<string> allFontFamilies;
        private List<int> allFontSizes;
        private List<string> allFontDecorations;


        public List<string> AllFields
        {
            get { return allFields; }
            set { allFields = value; this.FirePropertyChanged("AllFields"); }
        }

        public List<string> AllFontFamilies
        {
            get { return allFontFamilies; }
            set { allFontFamilies = value; this.FirePropertyChanged("AllFontFamilies"); }
        }

        public List<int> AllFontSizes
        {
            get { return allFontSizes; }
            set { allFontSizes = value; this.FirePropertyChanged("AllFontSizes"); }
        }

        public List<string> AllFontDecorations
        {
            get { return allFontDecorations; }
            set { allFontDecorations = value; this.FirePropertyChanged("AllFontDecorations"); }
        }
        
        public VmLayout(Template template)
        {
            business = new LayoutBusiness();
            SetDataService(business);
            LoadLayoutByTemplateId(template.Id);

            Template = template;


            LoadAllFields();
            LoadFontDefinitions();

        }

        private void LoadLayoutByTemplateId(int templateId)
        {
            Items = business.LoadByTemplateId(templateId);
        }

        private void LoadAllFields()
        {
            var stringPropertyNames = typeof (FieldDefinition)
                .GetProperties()
                .Where(pi => pi.PropertyType == typeof (string) && pi.GetGetMethod() != null)
                .Select(p => p.Name).ToList();
                    /*.Select(pi => new
                    {
                        Name = pi.Name,
                        Value = pi.GetGetMethod().Invoke(myObject, null)
                    }).ToList();*/
            AllFields = stringPropertyNames;
        }

        private void LoadFontDefinitions()
        {
            InstalledFontCollection fonts = new InstalledFontCollection();
            AllFontFamilies = fonts.Families.Select(p => p.Name).ToList();

            AllFontSizes = new List<int>();
            for (int i = 10; i < 60; i++)
            {
                AllFontSizes.Add(i);
            }


            List<string> datas = Enum.GetNames(typeof (System.Drawing.FontStyle)).ToList();
            AllFontDecorations = datas;

        }



        #region override

        public override void PreSave()
        {
            if (SelectedItem != null)
                SelectedItem.Template = Template;

            if (OnSaved != null)
                OnSaved(SelectedItem, EventArgs.Empty);

        }
        

        #endregion
    }
}
